/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include <string.h>
#include <jni.h>
#include <errno.h>
#include <android/log.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <dirent.h>


#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "apkinfro-jni", __VA_ARGS__))
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, "apkinfro-jni", __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "apkinfro-jni", __VA_ARGS__))

static JavaVM* g_javavm = NULL;

/*
 * Class:     com_example_remotebomb23_BombSetting
 * Method:    encryptPW
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
jstring Java_com_example_remotebomb23_BombSetting_encryptPW
  (JNIEnv * env, jobject thiz, jstring password) {
	// strBad is default return value
	jstring strBad = (*env)->NewStringUTF(env, "bad");
	// start to get phone device ID
	jclass clsContextWrapper = (*env)->FindClass(env, "android/content/ContextWrapper");
	if (clsContextWrapper == NULL) {
		LOGD("[encryptPW][got clsContextWrapper == NUL]");
		return strBad;
	}
	jmethodID midGetAppContext = (*env)->GetMethodID(env,clsContextWrapper,"getApplicationContext","()Landroid/content/Context;");
	if (midGetAppContext == NULL) {
		LOGD("[encryptPW][got midGetAppContext == NUL]");
		return strBad;
	}
	jobject objContext = (*env)->CallNonvirtualObjectMethod(env,thiz,clsContextWrapper,midGetAppContext);
	if (objContext == NULL) {
		LOGD("[encryptPW][got objContext == NUL]");
		return strBad;
	}
	jclass clsContext = (*env)->FindClass(env, "android/content/Context");
	jmethodID midGetSysService = (*env)->GetMethodID(env,clsContext,"getSystemService","(Ljava/lang/String;)Ljava/lang/Object;");
	jstring strPhone = (*env)->NewStringUTF(env, "phone");
	jobject objTM = (*env)->CallObjectMethod(env,objContext,midGetSysService,strPhone);
	if (objTM == NULL) {
		LOGD("[encryptPW][got objTM == NUL]");
		return strBad;
	}
	jclass clsTM = (*env)->FindClass(env, "android/telephony/TelephonyManager");
	jmethodID midGetDeviceID = (*env)->GetMethodID(env,clsTM,"getDeviceId","()Ljava/lang/String;");
	jstring strDeviceID = (*env)->CallObjectMethod(env,objTM,midGetDeviceID);
	if (strDeviceID == NULL) {
		LOGD("[encryptPW][got strDeviceID == NUL]");
		return strBad;
	}
	// start to encrypt input password with phone deviceID

	return strBad;
}

/*
 * Class:     com_example_remotebomb23_BombSetting
 * Method:    decryptPW
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
jstring Java_com_example_remotebomb23_BombSetting_decryptPW
  (JNIEnv * env, jobject obj, jstring password) {
	// strBad is default return value
	jstring strBad = (*env)->NewStringUTF(env, "bad");

	return strBad;
}
/*
* Set some test stuff up.
*
* Returns the JNI version on success, -1 on failure.
*/
//jint JNI_OnLoad(JavaVM* vm, void* reserved)
//{
//    JNIEnv* env;
//    LOGD("[JNI_OnLoad][In]");
//    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
//    	LOGD("[JNI_OnLoad][Check JNI version error, return -1]");
//    	return -1;
//    }
//    g_javavm = vm;
//    // Get jclass with env->FindClass.
//    // Register methods with env->RegisterNatives.
//    LOGD("[JNI_OnLoad][Out][return JNI_VERSION_1_6]");
//    return JNI_VERSION_1_6;
//}
